import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(Vuex)
Vue.use(VueAxios, axios)

export default new Vuex.Store({
    state: {
        devMode: true,
        operator: '',
        token: '',
        mainData: null,
        rosterData: null,
        userData: null, //name, c, s:[t0, t1, t2, t3, t4], details:[]
        isReady: false,
        type_strings: ['模拟机复训', '理论复训', '现场复训', '模拟机考核', '理论考核', '现场考核']
    },
    mutations: {
        verifyOperator(state, payload) {
            state;
            payload;
        },
        loadData(state, payload) {
            state.mainData = payload;
            state.isReady = true;
            if (state.devMode) {
                window.console.log(payload);
            }
        },
        loadRoster(state, payload) {
            state.mainData = payload;
            state.isReady = true;
            if (state.devMode) {
                window.console.log(payload);
            }
        },
        calcUserData(state) {
            for (let i = 0; i < state.rosterData.length; i++) {
                let userData = {
                    name: '',
                    summaries: [],
                    details: []
                };
                for (let l = 0; l < state.type_strings.length; l++) {
                    userData.summaries.push(0);
                }
                for (let j = 0; j < state.mainData.length; j++) {
                    if (state.mainData[j].name == state.rosterData[i]) {
                        userData.details.push(state.mainData[j]);
                        for (let k = 0; k < state.type_strings.length; k++) {
                            if (state.mainData[j].type == state.type_strings[k]) {
                                userData.summaries[k]++;
                            }
                        }
                    }
                }
            }

        },
        beforeLoaded(state) {
            state.isReady = false;
        }
    },
    actions: {
        loadData(context) {
            if (context.state.devMode) {
                //devData
                context.commit('loadData', [{ "identifier": "1", "name": "幺鸡", "date": "2019-12-06", "type": "1", "title": "相关进近", "duration": "1", "examiner": "", "time": "2019-12-06 17:44:59" }]
                );
                return;
            }
            context.commit('beforeLoaded');
            axios.post('backend.php', { action: 'load', payload: context.state.token }).then(
                res => { context.commit('loadData', res.data) }
            ).catch(err => window.console.log(err))
        },
        loadRoster(context) {
            if (context.state.devMode) {
                context.commit('loadRoster', ['幺鸡', '二条', '三桶', '四万'])
                return;
            }
            context.commit('beforeLoaded');
            axios.post('backend.php', { action: 'roster', payload: context.state.token }).then(
                res => { context.commit('loadRoster', res.data) }
            ).catch(err => window.console.log(err))
        }
    }
})